var prefix = jsctx + "/tenantUgsLevelType";

var tenantUgsLevelType = (function () {
    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        }, 10)
    };

    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });
    var h = $(window).height() - 80;

    function load() {
        $('#exampleTable')
            .bootstrapTable(
                {
                    method: 'get',
                    url: prefix + "/listPageTenantUgsLevelType",
                    iconSize: 'outline',
                    toolbar: '#exampleToolbar',
                    striped: false,
                    dataType: "json",
                    pagination: true,
                    singleSelect: false,
                    cache: false,
                    pageSize: 10,
                    pageList: [10, 20, 30],
                    pageNumber: 1,
                    sidePagination: "server",
                    height: h,
                    paginationShowPageGo: true,
                    // rightFixedColumns: true,
                    // showColumns: true,
                    queryParams: function (params) {
                        return {
                            limit: params.limit,
                            offset: (params.offset / params.limit) + 1
                        };
                    },
                    columns: [
                        {
                            title: '#', field: '', width: '50',
                            formatter: function (value, row, index) {
                                return index + 1;
                            }
                        },
                        {field: 'levelType', title: '等级类型', align: 'left'},
                        {field: 'levelTypeName', title: '等级名称', align: 'left'},
                        {
                            field: 'companyEmail', title: '成长值', align: 'left',
                            formatter: function (value, row, index) {
                                return '<span value="111">' + row.minValue + '~' + row.maxValue + '/季度</span>'
                            }
                        },
                        {field: 'keepValue', title: '保级值', align: 'left'},
                        {field: 'discountPercent', title: '服务费折扣', align: 'left'},
                        {
                            title: '操作',
                            field: 'roleId',
                            align: 'center',
                            formatter: function (value, row, index) {
                                var a = '';
                                if (MemberManager_tenantUgsLevel_update) {
                                    a = '<a class="btn btn-primary btn-sm ' + has_edit + '" href="#" title="编辑" onclick="tenantUgsLevelType.editTenantUgsLevelType(\''
                                        + row.ugsLevelTypeId
                                        + '\')">编辑</a> ';
                                }
                                return a;

                            }
                        }
                    ]
                });
    }

    function reLoad() {
        $('#exampleTable').bootstrapTable('refresh');
    }


    function editTenantUgsLevelType(ugsLevelTypeId) {
        location.href = prefix + "/editTenantUgsLevelType/" + ugsLevelTypeId;
    }

    function tenantUgsLevelGrowing() {
        location.href = prefix + "/tenantUgsLevelGrowing";
    }

    function tenantUgsLevelDiscountStation() {
        location.href = prefix + "/levelDiscountStation";
    }

    function tenantUgsLevelGain() {
        location.href = jsctx + "/tenantUgsLevelGain/ugsLevelType";
    }

    function goBack() {
        window.history.back();
    }


    return {
        init: init,
        goBack: goBack,
        reLoad: reLoad,
        editTenantUgsLevelType: editTenantUgsLevelType,
        tenantUgsLevelGrowing: tenantUgsLevelGrowing,
        tenantUgsLevelDiscountStation: tenantUgsLevelDiscountStation,
        tenantUgsLevelGain: tenantUgsLevelGain
    }
        ;
})();

var editTenantUgsLevelType = (function () {
    function init() {
        validateRule();
    };

    function getCheckedRoles() {
        var adIds = "";
        $("input:checkbox[name=role]:checked").each(function (i) {
            if (0 == i) {
                adIds = $(this).val();
            } else {
                adIds += ("," + $(this).val());
            }
        });
        return adIds;
    }

    var isajaxsend = true;

    function save() {
        if (isajaxsend) {
            var formData = new FormData($("#frmTenantUgsLevelType")[0]);
            $("#roleIds").val(getCheckedRoles());
            $.ajax({
                cache: true,
                type: "POST",
                url: prefix + "/saveOrUpdateTenantUgsLevelType",
                data: formData,
                processData: false,
                contentType: false,
                async: false,
                beforeSend: function () {
                    isajaxsend = false
                },
                error: function (request) {
                    parent.layer.alert("Connection error");
                },
                success: function (data) {
                    if (data.status == 200) {
                        parent.layer.msg("操作成功");
                        window.history.back();
                    } else {
                        parent.layer.alert(data.message)
                    }

                },
                complete: function () {
                    isajaxsend = false
                }
            });
        }
    }

    function validateRule() {
        var icon = "<i class='fa fa-times-circle'></i> ";
        $("#frmTenantUgsLevelType").validate({
            rules: {
                levelTypeName: {
                    required: true
                },
                expireDay: {
                    required: true
                },
                minValue: {
                    required: true
                },
                maxValue: {
                    required: true
                },
                keepValue: {
                    required: true
                },
                discountPercent: {
                    required: true,
                    min: 0,
                    max: 1
                }

            },
            messages: {
                levelTypeName: {
                    required: icon + "请输入等级名称"
                },
                expireDay: {
                    required: icon + "请输入有效期"
                },
                minValue: {
                    required: icon + "请输入最小经验值"
                },
                maxValue: {
                    required: icon + "请输入最大经验值"
                },
                keepValue: {
                    required: icon + "请输入保级值"
                },
                discountPercent: {
                    required: icon + "请输入服务费折扣",
                    min: icon + "最小值为0",
                    max: icon + "最大值为1"
                }
            },
            submitHandler: function (form) {
                save();
            }
        })
    }

    return {
        init: init
    };
})();


var editTenantUgsLevelType = (function () {
    function init() {
        validateRule();
    };

    function getCheckedRoles() {
        var adIds = "";
        $("input:checkbox[name=role]:checked").each(function (i) {
            if (0 == i) {
                adIds = $(this).val();
            } else {
                adIds += ("," + $(this).val());
            }
        });
        return adIds;
    }

    var isajaxsend = true;

    function save() {
        if (isajaxsend) {
            var formData = new FormData($("#frmTenantUgsLevelType")[0]);
            $("#roleIds").val(getCheckedRoles());
            $.ajax({
                cache: true,
                type: "POST",
                url: prefix + "/saveOrUpdateTenantUgsLevelType",
                data: formData,
                processData: false,
                contentType: false,
                async: false,
                beforeSend: function () {
                    isajaxsend = false
                },
                error: function (request) {
                    parent.layer.alert("Connection error");
                },
                success: function (data) {
                    if (data.status == 200) {
                        parent.layer.msg("操作成功");
                        window.history.back();
                    } else {
                        parent.layer.alert(data.message)
                    }

                },
                complete: function () {
                    isajaxsend = false
                }
            });
        }
    }

    function validateRule() {
        var icon = "<i class='fa fa-times-circle'></i> ";
        $("#frmTenantUgsLevelType").validate({
            rules: {
                levelTypeName: {
                    required: true
                },
                expireDay: {
                    required: true,
                    digits: true,
                },
                minValue: {
                    required: true,
                    digits: true,
                    min: 0
                },
                maxValue: {
                    required: true,
                    digits: true,
                    min: 0
                },
                keepValue: {
                    required: true,
                    digits: true,
                    min: 0
                },
                discountPercent: {
                    required: true,
                    min: 0.78,
                    max: 1
                }

            },
            messages: {
                levelTypeName: {
                    required: icon + "请输入等级名称"
                },
                expireDay: {
                    required: icon + "请输入有效期",
                    digits: icon + "请输入整数",
                },
                minValue: {
                    required: icon + "请输入最小经验值",
                    digits: icon + "请输入大于0的整数",
                    min: icon + "请输入大于0的整数"
                },
                maxValue: {
                    required: icon + "请输入最大经验值",
                    digits: icon + "请输入大于0的整数",
                    min: icon + "请输入大于0的整数"
                },
                keepValue: {
                    required: icon + "请输入保级值",
                    digits: icon + "请输入大于0的整数",
                    min: icon + "请输入大于0的整数"
                },
                discountPercent: {
                    required: icon + "请输入服务费折扣",
                    min: icon + "请输入0.78-1之间",
                    max: icon + "请输入0.78-1之间"
                }
            },
            errorPlacement: function (error, element) { //错误信息位置设置方法
                error.appendTo(element.parent()); //这里的element是录入数据的对象
            },
            submitHandler: function (form) {
                save();
            }
        })
    }

    return {
        init: init
    };
})();


var editStationDiscount = (function () {
    function init() {
        validateRule();
    };

    function getCheckedRoles() {
        var adIds = "";
        $("input:checkbox[name=role]:checked").each(function (i) {
            if (0 == i) {
                adIds = $(this).val();
            } else {
                adIds += ("," + $(this).val());
            }
        });
        return adIds;
    }

    var isajaxsend = true;

    function save() {
        if (isajaxsend) {
            var formData = new FormData($("#frmStationDiscount")[0]);
            $("#roleIds").val(getCheckedRoles());
            $.ajax({
                cache: true,
                type: "POST",
                url: prefix + "/saveOrUpdateStationDiscount",
                data: formData,
                processData: false,
                contentType: false,
                async: false,
                beforeSend: function () {
                    isajaxsend = false
                },
                error: function (request) {
                    parent.layer.alert("Connection error");
                },
                success: function (data) {
                    if (data.status == 200) {
                        parent.layer.msg("操作成功");
                        window.history.back();
                    } else {
                        parent.layer.alert(data.message)
                    }

                },
                complete: function () {
                    isajaxsend = false
                }
            });
        }
    }

    function validateRule() {
        var icon = "<i class='fa fa-times-circle'></i> ";
        $("#frmStationDiscount").validate({
            rules: {
                l1: {
                    required: true,
                    min: 0,
                    max: 1
                },
                l2: {
                    required: true,
                    min: 0,
                    max: 1
                },
                l3: {
                    required: true,
                    min: 0,
                    max: 1
                },
                l4: {
                    required: true,
                    min: 0,
                    max: 1
                },
                l5: {
                    required: true,
                    min: 0,
                    max: 1
                }

            },
            messages: {
                l1: {
                    required: icon + "请输入折扣",
                    min: icon + "折扣应该大于0",
                    max: icon + "折扣应该小于1"
                },
                l2: {
                    required: icon + "请输入折扣",
                    min: icon + "折扣应该大于0",
                    max: icon + "折扣应该小于1"
                },
                l3: {
                    required: icon + "请输入折扣",
                    min: icon + "折扣应该大于0",
                    max: icon + "折扣应该小于1"
                },
                l4: {
                    required: icon + "请输入折扣",
                    min: icon + "折扣应该大于0",
                    max: icon + "折扣应该小于1"
                },
                l5: {
                    required: icon + "请输入折扣",
                    min: icon + "折扣应该大于0",
                    max: icon + "折扣应该小于1"
                }
            },
            submitHandler: function (form) {
                save();
            }
        })
    }

    return {
        init: init
    };
})();